package com.cam.test.zip.controller;

import com.cam.framework.utils.RestResult;
import com.cam.test.zip.entity.ZipTestDirectory;
import com.cam.test.zip.entity.ZipTestFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@RestController
@RequestMapping("/test/zip")
public class ZipTestController {

    @PostMapping("/parse")
    public RestResult parse(MultipartFile multipartFile) throws IOException {
        if (multipartFile == null || multipartFile.isEmpty()) {
            return RestResult.error("没有文件被上传");
        }
        ZipTestDirectory rootDirectory = new ZipTestDirectory();
        rootDirectory.setId(UUID.randomUUID().toString().replace("-", ""));
        rootDirectory.setName("跟目录");

        List<ZipTestFile> fileList = new ArrayList<>();

        Map<String, ZipTestDirectory> directoryMap = new HashMap<>();
        directoryMap.put("123", rootDirectory);

        ZipInputStream zipInputStream = new ZipInputStream(multipartFile.getInputStream());
        ZipEntry zipEntry = null;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            System.out.println(zipEntry.isDirectory());
            System.out.println(zipEntry.getName());
            String entryName = zipEntry.getName();
            if (zipEntry.isDirectory()) {
                // 如果是目录的话，目录的结尾会带一个"/"，去掉
                String directoryFullName = entryName.substring(0, entryName.lastIndexOf("/"));
                if (directoryFullName.contains("/")) {
                    // 如果还有"/"的话，说明不是跟目录，就要寻找父级关系
                    String directoryName = directoryFullName.substring(directoryFullName.lastIndexOf("/") + 1);
                    ZipTestDirectory directory = new ZipTestDirectory();
                    directory.setId(UUID.randomUUID().toString());
                    directory.setName(directoryName);
                    String parentDirectoryName = directoryFullName.substring(0, directoryFullName.lastIndexOf("/"));
                    if (directoryMap.containsKey(parentDirectoryName)) {
                        ZipTestDirectory parentDirectory = directoryMap.get(parentDirectoryName);
                        parentDirectory.getChildren().add(directory);
                        directory.setParent(parentDirectory);
                    } else {
                        System.out.println("未找到父目录");
                    }
                    directoryMap.put(directoryFullName, directory);
                } else {
                    if (!directoryMap.containsKey(directoryFullName)) {
                        ZipTestDirectory directory = new ZipTestDirectory();
                        directory.setId(UUID.randomUUID().toString());
                        directory.setName(directoryFullName);
                        directoryMap.put(directoryFullName, directory);
                    }
                }
            } else {
                // 如果是文件的话
                if (entryName.contains("/")) {
                    // 如果有"/"，证明不是根目录下的文件，找它所在的目录
                    String directoryName = entryName.substring(0, entryName.lastIndexOf("/"));
                    String fileName = entryName.substring(entryName.lastIndexOf("/") + 1);
                    ZipTestFile file = new ZipTestFile();
                    file.setId(UUID.randomUUID().toString());
                    file.setName(fileName);
                    if (directoryMap.containsKey(directoryName)) {
                        file.setDirectory(directoryMap.get(directoryName));
                        fileList.add(file);
                    }
                }
            }
            /*if (zipEntry.isDirectory()) {
                System.out.println(zipEntry.getName());
            } else {
                System.out.println(zipEntry.getName());
            }*/
        }
        return RestResult.success();
    }

}
